﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeansWsccProject.App_Code;

namespace DeansWsccProject
{
    public partial class authors_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            functions.fillDataView(authorsGridView, "SELECT [profileUserName], [FirstName], [LastName], [Description], [Picture] FROM [profile] ORDER BY [LastName], [FirstName]", null);
        }

        protected void authorsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // check row is data row (not header or footer)
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // get profileUserName from row using datakeys
                string keyValue = authorsGridView.DataKeys[e.Row.RowIndex].Values[0].ToString();

                // hide row if user is not assigned to authors role
                if (functions.getDataFromTable("SELECT [RoleName] FROM [userNameRoleView] WHERE ([UserName] = @f1)", keyValue)[0] != "authors")
                {
                    e.Row.Visible = false;
                }
                // hide row of logged in author
                else if (User.Identity.Name == keyValue)
                {
                    e.Row.Visible = false;
                }
                // populate visible rows with ebooks repeater data
                else
                {
                    var rptImage = e.Row.FindControl("authoredEBooksRepeater") as Repeater;
                    if (rptImage != null)
                    {
                        functions.fillRepeater(rptImage, "SELECT [ID], [profileUserName], [eBookTitle], [eBookImage], [eBookReleaseDate] FROM [eBookAuthorView] WHERE ([profileUserName] = @f1) ORDER BY [eBookReleaseDate]", keyValue);
                    }
                }
            }
        }
    }
}